package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/21 20:06
 */
@SuppressWarnings("all")
@Service
public class DtsCouponServiceImpl implements DtsCouponService {
    @Autowired
    DtsCouponMapper dtsCouponMapper;

    /**
     * @param name 优惠卷标题
     * @param type  优惠卷类型
     * @param status    优惠卷时效类型
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String order, String sort, String name, Integer type, Integer status) {

        IPage<DtsCoupon> iPage = new Page<>(page,limit);

        QueryWrapper<DtsCoupon> wrapper = new QueryWrapper<>();

        if(name!=null){
            wrapper.like("name",name);
        }
        if(type!=null){
            wrapper.eq("type",type);
        }
        if(status!=null){
            wrapper.eq("status",status);
        }
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }

        wrapper.eq("deleted",false);
        dtsCouponMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());
        return map;
    }

    @Override
    public void insert(DtsCoupon dtsCoupon) {
        dtsCoupon.setAddTime(new Date());
        dtsCoupon.setUpdateTime(new Date());
        dtsCoupon.setDeleted(false);
        dtsCouponMapper.insert(dtsCoupon);
    }

    /**
     * 优惠劵详情
     */
    @Override
    public DtsCoupon findById(Integer id) {
        LambdaQueryWrapper<DtsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCoupon::getDeleted,false);
        wrapper.eq(DtsCoupon::getId,id);
        return dtsCouponMapper.selectOne(wrapper);
    }

    /**
     * 优惠券更新
     */
    @Override
    public void update(DtsCoupon dtsCoupon) {
        dtsCoupon.setUpdateTime(new Date());
        dtsCouponMapper.updateById(dtsCoupon);
    }

    /**
     * 优惠券删除
     */
    @Override
    public void delete(DtsCoupon dtsCoupon) {
        dtsCoupon.setDeleted(true);
        dtsCouponMapper.updateById(dtsCoupon);
    }
}
